package com.twu.biblioteca;

public class User {
    private final String username;
    private final String password;
    private final String email;
    private final String phone;


    public User(String username, String password, String email, String phone) {

        if(!this.isValidUsernamePattern(username) || password.equals(" ")){
            throw new RuntimeException("A user must have a username and password");
        }

        this.username = username;
        this.password = password;
        this.email = email;
        this.phone = phone;
    }

    public boolean credentialsMatch(String username, String password) {
        return username.equals(this.username) && password.equals(this.password);
    }

    public boolean isValidUsernamePattern(String username) {
        return username.matches("[0-9]{3}-[0-9]{4}");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        User user = (User) o;

        if (email != null ? !email.equals(user.email) : user.email != null) return false;
        if (password != null ? !password.equals(user.password) : user.password != null) return false;
        if (phone != null ? !phone.equals(user.phone) : user.phone != null) return false;
        if (username != null ? !username.equals(user.username) : user.username != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = username != null ? username.hashCode() : 0;
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (email != null ? email.hashCode() : 0);
        result = 31 * result + (phone != null ? phone.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Username : '" + username + "', email: '" + email +"' Phone: '" + phone +"'\n";
    }


    public String getUsername() {
        return username;
    }
}
